/*
 * Copyright (C) 2007,2008  Jonathan Hendler
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * 
 * The authors can be contacted at: 
 * @author Jonathan Hendler [jonathan DOT hendler AT google DOT com]
 * 
 * @license GPLv2
 */

/**
 * this file tests the basic NYRML framework by "sending"
 * messages and tracing their path. It is meant to serve as a reference implementation 
 * for the extern functions, as well as to provide a way to visually represent the
 * network and eventually the  propogation  of information through a simulated network.
 * 
 * DOT output
  
  graph HELLA {
	  node [shape=circle, width="0.5"];
	  1 -- 2 --5 [label="pc" color=blue ];
	  1 -- 3 -- 4 [label="pc" color=blue ];
	  4 -- 5 [label="p2p" color=red];
	  4 -- 4 [label="p2p" color=red];
	  2 -- 3 [label="p2p" color=red ];
	  { rank=same; 2 3 }
      { rank=same; 4 5 }
  }
http://www.graphviz.org/doc/info/attrs.html
 * 
 * */
#include "test.h"


static char nymrl_digraph[1024];
static int nymrl_current_graph_length = 1024;


char nymrl_test_get_char(){
	char input = getc(stdin);
	return (input >0) ? input : 'n';
}

 
 void NYMRL_RunAllTests(int test)
 {
 	CuString *output = CuStringNew();
 	CuSuite* suite = CuSuiteNew();
 	const char * testname = "null test";
 	
 	switch(test){
 	case 1:
 		CuSuiteAddSuite(suite, CuGetSuiteOne());
 		testname = "db tests";
 		break;
 	case 2:
 		//mc_set_debug(1);
 		CuSuiteAddSuite(suite, CuGetSuiteTwo());
 		testname = "REST RPC tests";
 		break;
 	case 3:
 		//CuSuiteAddSuite(suite, CuGetSuite());
 		break;
 	default:
 		NYMRL_TEST_OUTPUT_STRING("ERROR","TEST NOT FOUND");
 		break;
 	}

 	CuSuiteRun(suite);
 	CuSuiteSummary(suite, output);
 	CuSuiteDetails(suite, output);
 	NYMRL_TEST_OUTPUT_STRING("test completed",testname);
 	NYMRL_TEST_OUTPUT_STRING("test output",output->buffer);
 	
 }
 
 
int main(){
	
    //http://www.ncsu.edu/itd/hpc/Documents/timing.html
	  
	char input = 'n';
	char msg_buffer[512];
	int drop_table = 0;
	int ret = 0;
	int i;
	int iter = 1;
	
	sqlite3 *  db = nymrl_get_router_db();
	nymrl_sqlite_print_debug_info(db,"SELECT COUNT(*) AS count FROM node_x_url");
	nymrl_get_log_db();
	
	NYMRL_TEST_OUTPUT_STRING("Start","starting tests.");
	//interactive - truncate database?
	NYMRL_TEST_OUTPUT_STRING("USER","Should the databases be truncated?");
	
	input = nymrl_test_get_char();
	 
	
	if (input == 'y'){
		sprintf(msg_buffer,"you pressed %c - ARE YOU SURE?",input);
		NYMRL_TEST_OUTPUT_STRING("report",msg_buffer);
		//newline
		input = nymrl_test_get_char();
		input = nymrl_test_get_char();
		if (input == 'y'){
			NYMRL_TEST_OUTPUT_STRING("report","creating and truncating sqlite databases");
			drop_table = 1;
		}
	}
	
	NYMRL_TIMER_START;
	ret = nyml_setup_self(drop_table);
	 
	sprintf(msg_buffer,"nyml_setup_self() returned %d",ret);
	NYMRL_TEST_OUTPUT_STRING("report",msg_buffer);
	for(i=0;i<iter;++i){
		NYMRL_RunAllTests(1);
		NYMRL_RunAllTests(2);
	}
	NYMRL_TIMER_END; 
	time_t time_now= time(NULL);
	char * str_time  = malloc(1024);
	//struct tm *loctime = localtime (&time_now);
	//= ctime(&time_now) ;
	sprintf(str_time,"%s",ctime(&time_now));
	
	sprintf(msg_buffer,"ALL TESTS COMPLETED\n\tCompleted in %f seconds.\n\ton %s", total_time,str_time);
	//printf("Elapsed time: %u secs.\n", clock()/CLOCKS_PER_SEC);
	NYMRL_TEST_OUTPUT_STRING("report",msg_buffer);
	
	nymrl_log_message("Ran test");
	//nymrl_sqlite_print_all_nymrl_data();
	nymrl_sqlite_global_close();
	
	
	free(str_time);
	return 0;
}

